﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using System.IO;

public partial class FrontEnd_Dialog_UploadPhoto : System.Web.UI.Page
{
    protected BLO.Album albums;
    protected String userID;
    protected int albumID;
    public string message = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Session.Add("LoginUser", "HienTT");
        if (Session["LoginUser"] == null) return;
        else userID = Session["LoginUser"].ToString();
        uploadPicture();

    }
    protected void uploadPicture()
    {
        String kind = Request.Form["hddKindSubmit"];
        if (kind == "upload")
        {
            //Insert album if haven't
            if (albums == null) albums = new BLO.Album();
            String albumName = Request.Form.Get("txtAlbumName");
            String folderAlbum = Server.MapPath(ServerPath.UserData + "/" + userID + "/Photo");
            if (albumName != "")
            {
                DTO.Album item = new DTO.Album();
                item.Name = albumName;
                item.UserID = userID;
                albums.Info = item;
                albumID = albums.Insert();
            }
            else
            {
                albumID = int.Parse(Request.Form.Get("sltAlbum"));
            }
            if (!Directory.Exists(folderAlbum)) Directory.CreateDirectory(folderAlbum);
            string photoName = Request.Form["txtPictureName"];
            if (photoName == "") photoName = FileUpload1.FileName;
            BLO.Photo photo = new BLO.Photo();
            DTO.Photo itemPhoto = new DTO.Photo();
            itemPhoto.Name = photoName;
            itemPhoto.AlbumID = albumID;
            itemPhoto.UserID = userID;
            itemPhoto.Description = Request.Form["txtDescription"];
            itemPhoto.Link = "Photo/"+photoName;
            photo.Info = itemPhoto;
            int result = photo.Insert();
            if (result > -1)
            {
                FileUpload1.SaveAs(folderAlbum + "/" +FileUpload1.FileName);
                message = "Upload thành công";
            }
        }
    }
}
